package com.jackaltsc.mydemodatastorage.sqlite;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * SQLite数据库创建和版本管理的类
 */
public class SQLiteHelper extends SQLiteOpenHelper {

    //表名
    public static final String TABLENAME = "tableuser";
    //版本
    private static final int DATABASE_VERSION = 1;

    //构造函数
    public SQLiteHelper(Context context) {
        super(context, TABLENAME, null, DATABASE_VERSION);
    }

    // Called when the database is created for the first time
    @Override
    public void onCreate(SQLiteDatabase db) {

        //sql语句
        String sqlStr = "create table " +
                SQLiteHelper.TABLENAME +
                "(userid integer primary key autoincrement," +
                "username varchar(64) , " +
                "userpasswd varchar(64))";

        // 执行没有返回值的sql语句
        db.execSQL(sqlStr);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        //数据库版本升级 修改表添加字段userhome
        String sqlStr = "alter table " +
                SQLiteHelper.TABLENAME +
                " add userhome varchar(8)";
        // Called when the database needs to be upgraded
        db.execSQL(sqlStr);
    }

}
